package com.jiuyin.dianjing.ui.activity.setting;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.ui.activity.base.BaseActivity;
import com.jiuyin.dianjing.util.LogUtil;

import butterknife.BindView;

import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_NICK_NAME;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_SIGN;

/**
 * 修改昵称
 */
public class ModifyNickNameActivity extends BaseActivity {
    @BindView(R.id.edt_nick_name)
    public EditText mEdtNickName;
    @BindView(R.id.tv_num)
    public TextView mTvNum;
    @BindView(R.id.bt_submit)
    Button btSubmit;

    private int MAX_LEN;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_modify_nick_name;
    }

    @Override
    protected void initData() {
        String signText = getIntent().getStringExtra(KEY_NICK_NAME);
        if (!TextUtils.isEmpty(signText)) {
            mEdtNickName.setText(signText);
        }
        MAX_LEN = getResources().getInteger(R.integer.nick_name_max_length);
        mEdtNickName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                int size = s.length();
                if (size <= MAX_LEN) {
                    mTvNum.setText(String.valueOf(MAX_LEN - size));
                }
            }
        });
    }

    @Override
    protected void initView() {
        setTitle(R.string.setting_modify_nick_name);
        btSubmit.setOnClickListener(v -> {
            setResult(RESULT_OK, getIntent().putExtra(KEY_NICK_NAME, mEdtNickName.getText().toString()));
            finish();
        });
    }
}
